%%%---------------------------------------------------------------------------------------
%%% @author    Stuart Jackson <sjackson@simpleenigma.com> [http://erlsoft.org]
%%% @copyright 2006 - 2008 Simple Enigma, Inc. All Rights Reserved.
%%% @doc       Erlang OpenId Utilities
%%% @reference See <a href="http://erlsoft.org/modules/erlopenid" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlopenid.googlecode.com" target="_top">ErlOpenId Google Code Repository</a> for more information
%%% @version   0.0.1
%%% @since     0.0.1
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2008 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(erlopenid_util).
-author('sjackson@simpleenigma.com').

-export([normalize/1]).




%%-------------------------------------------------------------------------
%% @spec (String::list()) -> list()
%% @doc Normailze URI or XRI
%% @end
%%-------------------------------------------------------------------------
normalize(String) ->
	case split_scheme(String) of
		{error,bad_scheme} -> {error,bad_scheme};
		{error,no_scheme} -> normalize("http://" ++ String);
		xri -> normalize_xri(String);
		Scheme when Scheme == http; Scheme == https -> normalize_uri(String)
	end.

normalize_uri(URI) ->
	case string:tokens(URI,":/") of
		[Scheme,Host] -> Scheme ++ "://" ++ Host ++ "/";
		[Scheme,Host,User] ->  
			Slash = case string:right(URI,1) of
				"/" -> "/";
				_ -> []
			end,
			Scheme ++ "://" ++ Host ++ "/" ++ User ++ Slash
	end.

normalize_xri(XRI) ->
	case string:to_lower(string:left(XRI,6))  of
		"xri://" -> string:right(XRI,length(XRI) - 6);
		_ -> XRI
	end.


split_scheme(String) -> 
	case regexp:split(String,":") of
		{error,Reason} -> {error,Reason};
		{ok,[URI]} -> 
			case string:left(URI,1) of
				C when C == "="; C == "@"; C == "+"; C == "$"; C == "!"; C == "(" -> xri;
				_ -> {error,no_scheme}
			end;
		{ok,[S,_Rest]} -> 
			case list_to_atom(string:to_lower(S)) of
				Scheme when Scheme == http; Scheme == https; Scheme == xri -> Scheme;
				_ -> {error,bad_scheme}
			end
	end.
